//
//  UIImage+water.m
//  bitmap_image
//
//  Created by mac on 15-7-12.
//  Copyright (c) 2015年 duanan. All rights reserved.
//

#import "UIImage+water.h"

@implementation UIImage (water)

+ (instancetype)waterimage:(UIImage*)bgimage logo:(UIImage*)logo savename:(NSString*)name
{
    // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)
    UIGraphicsBeginImageContextWithOptions(bgimage.size, NO, 0.0);
    // 2.画背景
    [bgimage drawInRect:CGRectMake(0, 0, bgimage.size.width, bgimage.size.height)];
    // 3.画右下角的水印
    [logo drawInRect:CGRectMake(bgimage.size.width*0.7, bgimage.size.height*0.85, bgimage.size.width*0.27, bgimage.size.height*0.12)];
    // 4.从上下文中取得制作完毕的UIImage对象
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 5.结束上下文
    UIGraphicsEndImageContext();
   
    if (name!=nil )
    {
    // 6.将image对象压缩为PNG格式的二进制数据
    NSData *data = UIImagePNGRepresentation(newImage);
    //    UIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)
    // 7.获取文件路径
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",name]];
    //保存图片
    [data writeToFile:path atomically:YES];
    }
    return newImage;
}
@end
